package com.paw.ruoyi.admin.controller.account;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.paw.ruoyi.admin.common.annotion.SkipLogParam;
import com.paw.ruoyi.admin.common.annotion.SkipResponseLog;
import com.paw.ruoyi.admin.common.domain.PageData;
import com.paw.ruoyi.admin.common.domain.PageDomain;
import com.paw.ruoyi.admin.core.util.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.paw.ruoyi.admin.common.annotion.OperatorLog;
import com.paw.ruoyi.admin.common.constant.BusinessType;
import com.paw.ruoyi.admin.common.domain.Result;
import com.paw.ruoyi.admin.common.annotion.RequiresPermissions;
import com.paw.ruoyi.admin.domain.AppUser;
import com.paw.ruoyi.admin.service.IUserService;
import com.paw.ruoyi.admin.utils.ExcelUtil;


/**
 * 用户Controller
 *
 * @author admin
 * @date 2022-01-22
 */
@RestController
@RequestMapping("/api/account/appUser")
public class AppUserController {
  @Autowired
  private IUserService userService;

  /**
   * 查询用户列表
   */
  @RequiresPermissions("account:user:list")
  @GetMapping("/list")
  public Result<PageData<AppUser>> list(AppUser user) {

    PageDomain pageDomain = TableSupport.buildPageRequest();
    LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>(user);
    // TODO like, order by
    // queryWrapper.orderBy("createTime".equalsIgnoreCase(pageDomain.getOrderBy()),Boolean.parseBoolean(pageDomain.getIsAsc()),SysDictType::getCreateTime);
    Page<AppUser> page = userService.page(new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()), queryWrapper);
    return Result.page(page.getRecords(),page.getTotal());
  }

  /**
   * 导出用户列表
   */
  @SkipLogParam
  @SkipResponseLog
  @RequiresPermissions("account:user:export")
  @OperatorLog(title = "用户", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, AppUser user) {
    List<AppUser> list = userService.list(Wrappers.lambdaQuery(user));
    ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
    util.exportExcel(response, list, "用户数据");
  }

  /**
   * 获取用户详细信息
   */
  @RequiresPermissions("account:user:query")
  @GetMapping(value = "/detail/{id}")
  public Result getInfo(@PathVariable("id") Long id) {
    return Result.result(userService.getById(id));
  }

  /**
   * 新增用户
   */
  @RequiresPermissions("account:user:add")
  @OperatorLog(title = "用户", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  public Result<Boolean> add(@RequestBody AppUser user) {
    boolean success =  userService.save(user);
    return Result.result(success);
  }

  /**
   * 修改用户
   */
  @RequiresPermissions("account:user:edit")
  @OperatorLog(title = "用户", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  public Result<Boolean> edit(@RequestBody AppUser user) {
    boolean success =  userService.updateById(user);
    return Result.result(success);
  }

  /**
   * 删除用户
   */
  @RequiresPermissions("account:user:remove")
  @OperatorLog(title = "用户", businessType = BusinessType.DELETE)
  @PostMapping("/batchRemove/{ids}")
  public Result remove(@PathVariable List<Long> ids) {
    boolean success =  userService.removeBatchByIds(ids);
    return Result.result(success);
  }
}
